<?php

declare(strict_types=1);

namespace App\Services\Code;

use Illuminate\Support\Facades\Log;

class HttpResponse
{
    public const CODE_SUCCESS = 200; // 成功码
    public const CODE_FAIL    = 1090; // 异常

    public const CODE_TOKEN_GETERR     = 1001; // token 获取失败
    public const CODE_TOKEN_ERR        = 1002; // token 验证失败
    public const CODE_TOKEN_PARAMS_ERR = 1003; // token 参数异常
    public const CODE_TOKEN_EXP        = 1004; // token 已经过期

    public const CODE_PARAMS_ERR       = 1100; // 参数错误
    public const CODE_DATA_EMPTY       = 1101; // 数据为空
    public const CODE_PARAMS_NOT_ALLOW = 1102; // 参数不被允许

    public const CODE_WECHAT_CODE_EMPTY  = 1200; //code为空
    public const CODE_WECHAT_SESSION_ERR = 1201; //session获取异常

    public const CODE_VERIFY_WITHIN   = 1300; //验证码有效期内
    public const CODE_VERIFY_REDO     = 1301; //验证码重复发送
    public const CODE_VERIFY_TIMEOUT  = 1302; //验证码已经过期
    public const CODE_VERIFY_ERROR    = 1303; //验证码错误

    public const CODE_CITY_ID_MISSING = 1401; //缺少用户所属城市ID
    public const CODE_CITY_ID_INVALID = 1402; //用户所属城市ID非法

    /**
     * code码说明
     */
    public static function codeMap(): array
    {
        return [
            self::CODE_SUCCESS => '成功',
            self::CODE_FAIL    => '异常',

            self::CODE_TOKEN_GETERR     => 'token 获取失败',
            self::CODE_TOKEN_ERR        => 'token 验证失败',
            self::CODE_TOKEN_PARAMS_ERR => 'token 参数异常',
            self::CODE_TOKEN_EXP        => 'token 已经过期',

            self::CODE_PARAMS_ERR       => '参数错误',
            self::CODE_DATA_EMPTY       => '数据为空',
            self::CODE_PARAMS_NOT_ALLOW => '参数不被允许',

            self::CODE_WECHAT_CODE_EMPTY  => 'code为空',
            self::CODE_WECHAT_SESSION_ERR => 'session获取异常',

            self::CODE_VERIFY_WITHIN  => '验证码有效期内',
            self::CODE_VERIFY_REDO    => '验证码重复发送',
            self::CODE_VERIFY_TIMEOUT => '验证码已经过期',
            self::CODE_VERIFY_ERROR   => '验证码错误',
        ];
    }

    /**
     * resp success
     *
     * @return array
     */
    public static function respSuccess(int $code, ?array $data, string $message = 'success')
    {
        return ['code' => $code, 'message' => $message, 'data' => $data];
    }

    /**
     * resp fail
     *
     * @return array
     */
    public static function respFail(int $code, string $message = 'fail')
    {
        Log::error($message);

        return ['code' => $code, 'message' => $message];
    }
}
